#include "WorldListener.h"
#include "ContactObject.h"
#include "CharacterServer.h"
#include "GemServer.h"


MyWorldListener::MyWorldListener(ServerWorldObjects* s) {

	swo = s;

}

void MyWorldListener::BeginContact(b2Contact* contact) { 
	void* bodyUserDataA = contact->GetFixtureA()->GetBody()->GetUserData();
	void* bodyUserDataB = contact->GetFixtureB()->GetBody()->GetUserData();

	ContactObject* contactObjectA, *contactObjectB;
	if (bodyUserDataA) {
		contactObjectA = static_cast<ContactObject*>( bodyUserDataA );
		contactObjectA->startContact();
	}

	if (bodyUserDataB) {
		contactObjectB = static_cast<ContactObject*>( bodyUserDataB );
		contactObjectB->startContact();
	}

	if (bodyUserDataA && bodyUserDataB) {
		if (contactObjectA->getType() == PLAYER) {
			CharacterServer* player = static_cast<CharacterServer*>(contactObjectA);

			switch (contactObjectB->getType()) {
			case MONSTER :
				player->die();
				break;

			case DOOR :
				player->win();
				break;

			case GEM :
				player->collect(static_cast<GemServer*>(contactObjectB), swo);
				break;

			case KILLING_FLOOR :
				player->die();
				break;

			default :
				break;
			}
		} else if (contactObjectB->getType() == PLAYER) {
			CharacterServer* player = static_cast<CharacterServer*>(contactObjectB);

			switch (contactObjectA->getType()) {
			case MONSTER :
				player->die();
				break;

			case DOOR :
				player->win();
				break;

			case GEM :
				player->collect(static_cast<GemServer*>(contactObjectA), swo);
				break;
				
			case KILLING_FLOOR :
				player->die();
				break;

			default :
				break;
			}
		}

		
	}
	

} 

void MyWorldListener::EndContact(b2Contact* contact) { 
	void* bodyUserDataA = contact->GetFixtureA()->GetBody()->GetUserData();
	void* bodyUserDataB = contact->GetFixtureB()->GetBody()->GetUserData();
	ContactObject* contactObjectA, *contactObjectB;

	if (bodyUserDataA) {
		contactObjectA = static_cast<ContactObject*>( bodyUserDataA );
		static_cast<ContactObject*>( bodyUserDataA )->endContact();
	}

	if (bodyUserDataB) {
		contactObjectB = static_cast<ContactObject*>( bodyUserDataB );
		static_cast<ContactObject*>( bodyUserDataB )->endContact();
	}

	if (bodyUserDataA && bodyUserDataB) {
		if (contactObjectA->getType() == PLAYER && contactObjectB->getType() == KILLING_FLOOR) {
			CharacterServer* player = static_cast<CharacterServer*>(contactObjectA);


		} else if (contactObjectA->getType() == KILLING_FLOOR && contactObjectB->getType() == PLAYER) {
			CharacterServer* player = static_cast<CharacterServer*>(contactObjectB);


		}
	}
} 

void MyWorldListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) 
{ /* handle pre-solve event */ } 

void MyWorldListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) 
{ /* handle post-solve event */ } 